package com.example.zhongxiangmo;



import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ErJiFragment extends Fragment {


    private View view;
    private ExpandableListView mElv;
    private String sUrl = "https://www.wanandroid.com/tree/json";
    private ArrayList<ErBean.DataBean> list;
    private ErlAdapter erlAdapter;
    private ProgressBar mPb;

    public ErJiFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_er_ji, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        mElv = (ExpandableListView) inflate.findViewById(R.id.elv);
        mPb = (ProgressBar) inflate.findViewById(R.id.pb);

        list = new ArrayList<>();

        erlAdapter = new ErlAdapter(getActivity(), list);


        mElv.setAdapter(erlAdapter);
        initData();

    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(sUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int code = connection.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK) {
                        InputStream is = connection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String len = null;
                        StringBuffer sbf = new StringBuffer();
                        while ((len = br.readLine()) != null) {
                            sbf.append(len);
                        }
                        is.close();
                        br.close();
                        String json = sbf.toString();
                        Gson gson = new Gson();
                        ErBean erBean = gson.fromJson(json, ErBean.class);
                        final List<ErBean.DataBean> data = erBean.getData();

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                list.addAll(data);
                                erlAdapter.notifyDataSetChanged();
                                mPb.setVisibility(View.GONE);
                            }
                        });

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
